#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "debug.h"
#include "os_api.h"
#include "i2c.h"
#include "hal/gpio.h"
#include "uart.h"
#include "debug.h"
#include "dev_api.h"
#include "audio_api.h"

#define _TASK_STACK_SIZE	(1024*3)

static void* _task_stack;

static OSTaskRef _task_ref = NULL;

static OSATimerRef _timer_ref = NULL;

static OSFlagRef _flag_ref = NULL;

#define TASK_TIMER_CHANGE_FLAG_BIT    0x01

static void _timer_callback(UINT32 tmrId)
{
    OSAFlagSet(_flag_ref, TASK_TIMER_CHANGE_FLAG_BIT, OSA_FLAG_OR);
}

//tts输入为unicode小端数据
const char tts_data[] = {0xed,0x8b,0xf3,0x97,0x6c,0x8f,0x62,0x63,0x4b,0x6d,0xd5,0x8b,0xed,0x8b,0xf3,0x97,0x6c,0x8f,0x62,0x63,0x4b,0x6d,0xd5,0x8b,0xed,0x8b,0xf3,0x97,0x6c,0x8f,0x62,0x63,0x4b,0x6d,0xd5,0x8b};

int tts_data_len = sizeof(tts_data);

static OSFlagRef tts_flag_ref = NULL;

static int sdk_tts_gpio = 0;

void sdk_tts_pa_on(void)
{
	hal_GpioSetDir(sdk_tts_gpio, GPIO_OUT_PIN);
	hal_GpioOut(sdk_tts_gpio, 1);
}
void sdk_tts_pa_off(void)
{
	hal_GpioSetDir(sdk_tts_gpio, GPIO_OUT_PIN);
	hal_GpioOut(sdk_tts_gpio, 0);	
}

static void tts_play_callback(int ret, int userdata)
{
	LOG_PRINTF("tts done \n");
	sdk_tts_pa_off();
	if (tts_flag_ref)
		OSAFlagSet(tts_flag_ref, 0x01, OSA_FLAG_OR);
}

static void _task(void *ptr)
{
    OSA_STATUS status;
    UINT32 flag_value;
    UINT32 flag_mask = TASK_TIMER_CHANGE_FLAG_BIT;
		
	if (dev_GetChipId() == 0x1602){
		// 1602PCM外挂TM8211时，yuge_tts_tm8211_init() 针对TM8211芯片的相关初始化
		yuge_tts_tm8211_init();	

		hal_GpioSetDir(1, GPIO_OUT_PIN);
		hal_GpioOut(1, 1);	// 调试demo板的配置
	} else if (dev_GetChipId() == 0x1606){
		// PCM
		//AudioHAL_AifUseSSPA();	// 切换使用PCM语音

		// internalCodec
		AudioHAL_AifUseInternalCodec();	// 切换使用模拟语音
	}

    status = OSAFlagCreate(&tts_flag_ref);
    ASSERT(status == OS_SUCCESS);
	
	LOG_PRINTF("_task Start...........\n");
	
    while(1) {
        status = OSAFlagWait(_flag_ref, flag_mask, OSA_FLAG_OR_CLEAR, &flag_value, OSA_SUSPEND);
		ASSERT(status == OS_SUCCESS);
        if (flag_value & TASK_TIMER_CHANGE_FLAG_BIT) {
            static int count = 0;
            count++;
			LOG_PRINTF("_task: 11count: %d\n", count);
			if (dev_GetChipId() == 0x1606){
				// internalCodec 调节音量
				AudioHAL_AifSetVolume(8);
			}			
			if(count % 3 == 0){				
				yuge_tts_speed(TTS_SPEED_MIN);	
				sdk_tts_pa_on();
				yuge_tts_start((char *)tts_data, tts_data_len, tts_play_callback, 0);						
			}
			else if(count % 3 == 1){
							
				yuge_tts_speed(TTS_SPEED_NORMAL);	
				// 阻塞播放	
				do{
					UINT32 tts_flag_value;
					sdk_tts_pa_on();
					yuge_tts_start((char *)tts_data, tts_data_len, tts_play_callback, 0);
					// 等待播放结束回调setflag，或者10s超时（这个时间根据实际情况设置，防止卡死）
					OSAFlagWait(tts_flag_ref, 0x01, OSA_FLAG_OR_CLEAR, &tts_flag_value, 200*10);
				}while(0);
				// 阻塞播放	
				do{
					UINT32 tts_flag_value;
					sdk_tts_pa_on();
					yuge_tts_start((char *)tts_data, tts_data_len, tts_play_callback, 0);	
					// 等待播放结束回调setflag，或者10s超时（这个时间根据实际情况设置，防止卡死）
					OSAFlagWait(tts_flag_ref, 0x01, OSA_FLAG_OR_CLEAR, &tts_flag_value, 200*10);	
				}while(0);
				// 阻塞播放	
				do{
					UINT32 tts_flag_value;
					sdk_tts_pa_on();
					yuge_tts_start((char *)tts_data, tts_data_len, tts_play_callback, 0);	
					// 等待播放结束回调setflag，或者10s超时（这个时间根据实际情况设置，防止卡死）
					OSAFlagWait(tts_flag_ref, 0x01, OSA_FLAG_OR_CLEAR, &tts_flag_value, 200*10);	
				}while(0);
			}
			else if(count % 3 == 2){
				
				yuge_tts_speed(TTS_SPEED_MAX);
				sdk_tts_pa_on();				
				yuge_tts_start((char *)tts_data, tts_data_len, tts_play_callback, 0);
			}
        }
    }
}

int main(void)
{
    int ret;

    ret = OSAFlagCreate(&_flag_ref);
    ASSERT(ret == OS_SUCCESS);

    ret = OSATimerCreate(&_timer_ref);
    ASSERT(ret == OS_SUCCESS);

	_task_stack = malloc(_TASK_STACK_SIZE);
	ASSERT(_task_stack != NULL);

    ret = OSATaskCreate(&_task_ref, _task_stack, _TASK_STACK_SIZE, 88, "test-task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);

	LOG_PRINTF("OSATimerStart\n");

    OSATimerStart(_timer_ref, 1 * 200, 15 * 200, _timer_callback, 0); // 10 seconds timer

	return 0;
}